package com.microfch.v;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.microfch.m.Question;
import com.microfch.ninetypetest.R;
import com.microfch.p.MainActivityP;

import java.util.ArrayList;
import java.util.List;



public class QuestionAdpter extends RecyclerView.Adapter implements View.OnClickListener {
    private List<Question> mQuestionList =null;
    public final String TAG = this.getClass().getSimpleName();
    public QuestionAdpter(List<Question> questionList) {
        mQuestionList = questionList;

    }



    @Override
    public void onClick(View v) {
        MainActivityP.getInstance().playChose();
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_item,parent,false);
        final RecyclerView.ViewHolder holder = new ViewHolder(view);
        ((ViewHolder) holder).btYes.setOnClickListener(this);
        ((ViewHolder) holder).btNo.setOnClickListener(this);
        ((ViewHolder) holder).btYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
                final int position = holder.getAdapterPosition();
                final boolean res = isChecked;
                if(buttonView.equals(((ViewHolder) holder).btYes)|| buttonView.equals(((ViewHolder) holder).btNo)){
                    Log.d(TAG,"on time cliecked btYes  btNo!!");
                }else {
                    return;
                }
                Question question = mQuestionList.get(position);
                question.setSwitchAnswer(isChecked);
                ((ViewHolder) holder).answer.setText(question.getAnswerContent());
                if(isChecked) {
                    ((ViewHolder) holder).answer.setTextColor(view.getResources().getColor(R.color.colorPrimary));
                    ((ViewHolder) holder).question.setTextColor(view.getResources().getColor(R.color.colorPrimary));
                    ((ViewHolder) holder).btYes.setTextColor(view.getResources().getColor(R.color.colorPrimary));
                }else {
                    ((ViewHolder) holder).answer.setTextColor(view.getResources().getColor(R.color.colorBlack));
                    ((ViewHolder) holder).question.setTextColor(view.getResources().getColor(R.color.colorBlack));
                    ((ViewHolder) holder).btYes.setTextColor(view.getResources().getColor(R.color.colorBlack));

                }

             //   Toast.makeText(view.getContext(),res?R.string.answer_yes:R.string.answer_no,Toast.LENGTH_SHORT).show();
             //   Log.d(TAG,"on time cliecked Toast !!");
            }
        });
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Question question = mQuestionList.get(position);
        ViewHolder h = (ViewHolder) holder;
        h.answer.setText(question.getAnswerContent());
        h.question.setText(question.getContent());
        h.answer_swtich.setChecked(question.getAnswer());
        if(question.getAnswer()) {
            h.btYes.setChecked(true);
            h.btNo.setChecked(false);
        }
        else {
            h.btYes.setChecked(false);
            h.btNo.setChecked(true);
        }
    }

    @Override
    public int getItemCount() {
        return mQuestionList.size();
    }


    static  class ViewHolder extends RecyclerView.ViewHolder {
        TextView question;
        TextView answer;
        Switch answer_swtich;
        RadioButton btYes;
        RadioButton btNo;
        public ViewHolder(View view) {
            super(view);
            question = (TextView)view.findViewById(R.id.question);
            answer = (TextView)view.findViewById(R.id.answer);
            answer_swtich = (Switch)view.findViewById(R.id.switch_answer);
            btYes = (RadioButton)view.findViewById(R.id.radioButtonYes);
            btNo = (RadioButton)view.findViewById(R.id.radioButtonNo);

        }
    }
}
